<?php include('./templete/public/head.php') ?>

<body>

<hr class="bgcolor"/>
<!--发布金点子-->
<div class="fabu-main fabu-idea-main">
    <div class="main-content">
        <div class="c-list">
            <form id="myform">
                <ul class="comm-table-view">
                    <li class="table-view-cell">
                        <a>
                            <span class="title">标题</span>
                            <span class="line">|</span>
                            <span class="input"><input type="text" name="title" id="" value="" placeholder="标题不少于三个字符"/></span>
                            <span class="fr"><i class="iconfont icon-youjiantou"></i></span>
                        </a>
                    </li>
                    <li class="table-view-cell">
                        <a>
                            <span class="title">部门</span>
                            <span class="line">|</span>
                            <span class="input" id="showDepartPicker"><input type="text" name="department"
                                                                             id="departName" value="" placeholder=""
                                                                             readonly/></span>
                            <span class="fr"><i class="iconfont icon-youjiantou"></i></span>
                        </a>
                    </li>
                    <li class="table-view-cell">
                        <a>
                            <span class="title">请选择分类</span>
                            <span class="line">|</span>
                            <span class="input" id="showClassPicker"><input type="text" name="question_classfy"
                                                                            id="className" value="" placeholder=""
                                                                            readonly/></span>
                            <span class="fr"><i class="iconfont icon-youjiantou"></i></span>
                        </a>
                    </li>
                    <li class="table-view-cell">
                        <a>
                            <span class="title">完成方式</span>
                            <span class="line">|</span>
                            <span class="input" id="showModePicker"><input type="text" name="complete_type"
                                                                           id="modeName" value="" placeholder=""
                                                                           readonly/></span>
                            <span class="fr"><i class="iconfont icon-youjiantou"></i></span>
                        </a>
                    </li>
                    <li class="table-view-cell partner" >
                        <a>
                            <span class="title">合作人员</span>
                            <span class="line">|</span>
                            <span class="input">
                                <input type="text" name="complete_type" id="hezuoPeople" value=""placeholder="" readonly/>
                                <input type="hidden" name="partmers_id" id="partmers_id" value=""placeholder="" readonly/>
                            </span>
                            <span class="fr"><i class="iconfont icon-youjiantou"></i></span>
                        </a>
                    </li>
                    <li class="table-view-cell table-view-cell2">
                        <a>
                            <span>存在问题</span>
                            <div class="fabu-text">
                                <textarea id="content"></textarea>
                            </div>
                            <div class="fabu-upload">
                                <img src="_images/icon/upload.jpg" class="showPopover _img_upload"/>
                            </div>
                        </a>
                    </li>
                    <li class="table-view-cell table-view-cell2">
                        <a>
                            <span>解决的方法</span>
                            <div class="fabu-text">
                                <textarea id="content"></textarea>
                            </div>
                            <div class="fabu-upload">
                                <img src="_images/icon/upload.jpg" class="showPopover _img_upload"/>
                            </div>
                        </a>
                    </li>
                </ul>
            </form>
            <div class="comm-btn comm-btn2 fabu-btn">
                <span class="color1 publish">提交</span>
                <!--                <span class="color2">越级提交</span>-->
            </div>
        </div>
    </div>

</div>


<div id="peopleList" style="display:none;">
    <!-- 已选择人员 -->
    <div class="select-people">
        <a data-id="123">刘海平<i class="del-btn iconfont">&#xe7be;</i></a>
        <a data-id="123">刘海平<i class="del-btn iconfont">&#xe7be;</i></a>
        <a data-id="123">刘海平<i class="del-btn iconfont">&#xe7be;</i></a>
        <a data-id="123">刘海平<i class="del-btn iconfont">&#xe7be;</i></a>
    </div>
    <div class="list-select">
        <lable>选择部门</lable>
        <select id="department_list" lay-verify="required" onchange="get_user_list_by_department_id()">

        </select>
    </div>
    <div class="list-name" id="_userlist">
        <p data-id="1"><i class="iconfont">&#xe672;</i>刘海平</p>
        <p><i class="iconfont">&#xe672;</i>刘海平</p>
        <p><i class="iconfont">&#xe672;</i>刘海平</p>
        <p><i class="iconfont">&#xe672;</i>刘海平</p>
        <p><i class="iconfont">&#xe672;</i>刘海平</p>
        <p><i class="iconfont">&#xe672;</i>刘海平</p>
    </div>
</div>

</body>
<script src="_js/plug/mui.picker.all.js"></script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script>

    $('.publish').click(function () {
        var param = $('#myform').serializeArray();
        console.log(param);
    })


    <?php
    $time = time();
    $rand = rand(0000000,9999999);
    $request = \think\Request::instance();
    $url =$request->domain().$request->url();
    $signature = 'jsapi_ticket='.get_jsapi_ticket()['ticket'].'&noncestr='.$rand.'&timestamp='.$time.'&url='.$url;
    $signature = sha1($signature);
    ?>
    wx.config({
        beta: true,// 必须这么写，否则wx.invoke调用形式的jsapi会有问题
        debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
        appId: '<?php echo get_appId()?>', // 必填，企业微信的corpID
        timestamp: <?php echo $time?>, // 必填，生成签名的时间戳
        nonceStr: '<?php echo $rand?>', // 必填，生成签名的随机串
        signature: '<?php echo $signature?>',// 必填，签名，见 附录-JS-SDK使用权限签名算法
        jsApiList: ['selectEnterpriseContact'] // 必填，需要使用的JS接口列表，凡是要调用的接口都需要传进来
    });

    function select_entercontact(_partmers_id){
        wx.invoke("selectEnterpriseContact", {
                "fromDepartmentId": -1,// 必填，表示打开的通讯录从指定的部门开始展示，-1表示自己所在部门开始, 0表示从最上层开始
                "mode": "multi",// 必填，选择模式，single表示单选，multi表示多选
                "type": ["user"],// 必填，选择限制类型，指定department、user中的一个或者多个
                //"selectedDepartmentIds": ["2","3"],// 非必填，已选部门ID列表。用于多次选人时可重入，single模式下请勿填入多个id
                "selectedUserIds": _partmers_id// 非必填，已选用户ID列表。用于多次选人时可重入，single模式下请勿填入多个id
            },function(res){
                if (res.err_msg == "selectEnterpriseContact:ok")
                {
                    if(typeof res.result == 'string')
                    {
                        res.result = JSON.parse(res.result) //由于目前各个终端尚未完全兼容，需要开发者额外判断result类型以保证在各个终端的兼容性
                    }
                    var selectedDepartmentList = res.result.departmentList;// 已选的部门列表
                    // for (var i = 0; i < selectedDepartmentList.length; i++)
                    // {
                    //     var department = selectedDepartmentList[i];
                    //     var departmentId = department.id;// 已选的单个部门ID
                    //     var departemntName = department.name;// 已选的单个部门名称
                    // }
                    var selectedUserList = res.result.userList; // 已选的成员列表
                    var userId=Array();
                    var userName=Array();
                    var userAvatar=Array();
                    for (var i = 0; i < selectedUserList.length; i++)
                    {
                        var user = selectedUserList[i];
                        userId[i] = user.id; // 已选的单个成员ID
                        userName[i] = user.name;// 已选的单个成员名称
                        userAvatar[i]= user.avatar;// 已选的单个成员头像
                    }

                    //var userid = userId.join('|');
                    //var username = username.join(' ');
                    alert(userName);
                    $("#hezuoPeople").val(userName);
                    $("#partmers_id").val(userId);
                }
            }
        );
    }


    //PopPicker选择器
    //var subinvman_sel1 = $("#showCompanyPicker");//公司
    var subinvman_sel2 = $("#showDepartPicker");//部门
    var subinvman_sel3 = $("#showClassPicker");//请选择分类
    var subinvman_sel4 = $("#showModePicker");//完成方式
    if (subinvman_sel2) {
        var picker2 = new mui.PopPicker();
        picker2.setData([{value: '1', text: 'IT部'}, {value: '2', text: '销售部'}]);
        document.querySelector("#showDepartPicker").addEventListener("tap", function () {
            picker2.show(function (items) {
                console.log(items[0].text)
                $("#departName").val(items[0].text);

            });
        });
    }
    if (subinvman_sel3) {
        var picker3 = new mui.PopPicker();
        picker3.setData([{value: '1', text: '产品质量'}, {value: '2', text: '产品成本'}, {value: '3', text: '产品性能'}]);
        document.querySelector("#showClassPicker").addEventListener("tap", function () {
            picker3.show(function (items) {
                console.log(items[0].text)
                $("#className").val(items[0].text);

            });
        });
    }

    if (subinvman_sel4) {
        var picker4 = new mui.PopPicker();
        picker4.setData([{value: '1', text: '我提你改'}, {value: '2', text: '自提自改'}, {value: '3', text: '寻求合作'}]);
        document.querySelector("#showModePicker").addEventListener("tap", function () {
            picker4.show(function (items) {
                console.log(items[0].text)
                $("#modeName").val(items[0].text);
                //todo  改完变成3
                if (items[0].value == 3) {
                    $('.partner').show();
                } else {
                    $('.partner').hide();
                    ;
                }

            });
        });
    }

    layui.use('form', function () {
        var form = layui.form;
        $("#hezuoPeople").on("click", function () {
            var partmers_id = $('#partmers_id').val();
            _partmers_id = partmers_id.split(',');
            select_entercontact(_partmers_id);

            /*
            layer.open({
                type: 1,
                title: '合作人员',
                area: ['100%', '100%'], //宽高
                content: $("#peopleList"),
                shade: [0.5, '#000']
            });
            form.render();
            */

        })
    });

    $(function () {
        $.ajax({
            url:'<?php echo url('user/get_department_list')?>',
            success:function (data) {
                console.log(data);
                if(data.code==0){
                    var department_html = ''
                    $.each(data.data,function (k,v) {
                        department_html+='<option value="'+v.id+'">'+v.name+'</option>'
                    })
                    $('#department_list').html(department_html);
                    get_user_list_by_department_id();
                }
            }
        })
    });

    function get_user_list_by_department_id() {
        console.log($('#department_list').val());
        var department_id = $('#department_list').val();
        $.ajax({
            url:"<?php echo url('user/get_userlist_by_departmentid')?>",
            data:{
                depart_id:department_id,
            },
            success:function (data) {
                console.log(data);
                var userlist_html ='';
                if(data.code==0){
                    $.each(data.data,function (k,v) {
                        userlist_html+='<p  data-id="'+v.userid+'" onclick="put_up(\''+v.userid+'\',\''+v.name+'\')"><i class="iconfont">&#xe672;</i>'+v.name+'</p>';
                    })
                    $("#_userlist").html(userlist_html);
                }
            }
        })
    }

    function put_up(userid,name) {
        var oSepeple = $('.select-people').find('a');
        var up_flag = false
        $.each(oSepeple,function (k,v) {
            var has_userid =$(v).attr('data-id');
            if(has_userid!==userid){
                up_flag = true;
            }
        });
        if(up_flag) {
            var html = '<a data-id="' + userid + '">' + name + '<i class="del-btn iconfont">&#xe7be;</i></a>';
            $('.select-people').append(html)
        }
    }
</script>

</html>